//
//  PKRootLeftViewController.m
//  Awhile
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKRootLeftViewController.h"
#import "PKRootRithtViewController.h"
#import "PKPlayerManger.h"
#import "UIImageView+WebCache.h"
#import "ChildListModel.h"
#import "PKPlayerManger.h"
#import "MusicPlayViewController.h"
#import "RootLeftCell.h"
@interface PKRootLeftViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong)PKRootRithtViewController *rootRightVC;
@property(nonatomic, strong)UITableView *mainListTableView; // 主列表
@property(nonatomic, strong)NSArray *dataSourceArray;// 提供数据源
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *descLabel;
@property(nonatomic, strong)UIImageView *musicimg;
@property(nonatomic, strong)UIView *radioView;
@property(nonatomic, strong)UIButton *playButton;//播放按钮
@property(nonatomic, strong)NSMutableArray *cellArr;//装图片的数组
@end

@implementation PKRootLeftViewController

-(NSArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = @[@"有声书",@"音乐",@"儿童",@"历史",@"电影"];
    }
    return _dataSourceArray;
}
-(NSMutableArray *)cellArr
{
    if (!_cellArr) {
        self.cellArr = [NSMutableArray arrayWithObjects:@"shu",@"yinlesongchi",@"ertong",@"book",@"dianying", nil];
    }
    return _cellArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 添加右侧视图控制器到左侧视图控制器
    UIImageView *backImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [backImageView setImage:[UIImage imageNamed:@"defaultCover"]];
    _mainListTableView.backgroundView = backImageView;
    UIImageView *cellBeijingImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [cellBeijingImage setImage:[UIImage imageNamed:@"cellBeijing"]];
    [self.view addSubview:cellBeijingImage];
    
    _mainListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 20)];
    _mainListTableView.dataSource = self;
    _mainListTableView.delegate = self;
    _mainListTableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    _mainListTableView.scrollEnabled = NO;
    _mainListTableView.backgroundColor = [UIColor clearColor];
    [_mainListTableView registerClass:[RootLeftCell class] forCellReuseIdentifier:@"rootLeftCell"];
    _mainListTableView.separatorStyle = NO;
    [self.view addSubview:_mainListTableView];
    
   //下方播放音乐视图
    [self createRadioView];

    //通知
    NSNotificationCenter *notifi = [NSNotificationCenter defaultCenter];
    [notifi addObserver:self selector:@selector(playMusic:) name:@"play" object:nil];
    
    _rootRightVC = [[PKRootRithtViewController alloc]init];
    [self.view addSubview:_rootRightVC.view];
  
    
}

-(void)createRadioView
{
    _radioView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-70, [UIScreen mainScreen].bounds.size.width, 70)];
    _radioView.backgroundColor = [UIColor colorWithRed:0.127 green:0.157 blue:0.054 alpha:1.000];
    //播放图片
    _musicimg = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    _musicimg.backgroundColor = [UIColor whiteColor];
    [_musicimg setImage:[UIImage imageNamed:@"iconfont-v24bofangpress"]];
    _musicimg.userInteractionEnabled = YES;
    UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(backToPlayView)];
    [_musicimg addGestureRecognizer:recognizer];
    [_radioView addSubview:_musicimg];
    [self.view addSubview:_radioView];
    
    //播放标题
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(65, 15, SCREEN_WIDTH-170, 40)];
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = [UIFont systemFontOfSize:13];
    _titleLabel.textColor = [UIColor whiteColor];
    [_radioView addSubview:_titleLabel];
    
    //播放暂停按钮
    self.playButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.playButton.frame = CGRectMake(SCREEN_WIDTH-100, 20, 40, 40);
    [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang"]forState:(UIControlStateNormal)];
    [self.playButton addTarget:self action:@selector(play:) forControlEvents:(UIControlEventTouchUpInside)];
    self.playButton.selected = NO;
    [_radioView addSubview:self.playButton];
    
    
    
}
-(void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]postNotificationName:@"changeButtonImage" object:nil userInfo:nil];
}

-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeButtonIamge) name:@"changeButtonImage" object:nil];
}

-(void)changeButtonIamge
{

    if ([PKPlayerManger shareManger].avPlayer.rate == 0.0 ) {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang"]forState:(UIControlStateNormal)];
    }else
    {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontzanting"]forState:(UIControlStateNormal)];
    }
    
}

-(void)backToPlayView
{

   
     MusicPlayViewController * playVC = [PKPlayerManger shareManger].musicPlayVC;
    if (playVC==nil) {
        return;
    }
    [self presentViewController:playVC animated:YES completion:nil];
  }

//通知
- (void)playMusic:(NSNotification *)test
{
    _titleLabel.text = test.userInfo[@"title"];
    [_musicimg sd_setImageWithURL:[NSURL URLWithString:test.userInfo[@"pic"]]];
}
//播放
-(void)play:(UIButton *)button
{
    
    if (_titleLabel.text == nil) {
        return;
    }
    [[PKPlayerManger shareManger] playAndPause];
    if ([PKPlayerManger shareManger].avPlayer.rate == 0.0 ) {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontbofang"]forState:(UIControlStateNormal)];
    }else
    {
        [self.playButton setImage:[UIImage imageNamed:@"iconfont-iconfontzanting"]forState:(UIControlStateNormal)];
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RootLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rootLeftCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = self.dataSourceArray[indexPath.row];
    cell.iconImage.image = [UIImage imageNamed:self.cellArr[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.titleLabel.textColor = [UIColor whiteColor];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_rootRightVC changeViewIndex:indexPath.row];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}







/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
